#include "Locker.hpp"

Sem::Sem()
{
    if(sem_init(&m_sem,0,0)!=0)
    {
        throw std::exception();
    }
}

Sem::Sem(int _num)
{
    if(sem_init(&m_sem,0,_num)!=0)
    {
        throw std::exception();
    }
}

Sem::~Sem()
{
    sem_destroy(&m_sem);
}

bool Sem::Wait()
{
    return sem_wait(&m_sem) == 0;
}

bool Sem::Post()
{
    return sem_post(&m_sem) == 0;
}

Locker::Locker()
{
    if(pthread_mutex_init(&m_mutex,NULL)!=0)
    {
        throw std::exception();
    }
}

Locker::~Locker()
{
    pthread_mutex_destroy(&m_mutex);
}

bool Locker::Lock()
{
    return pthread_mutex_lock(&m_mutex) == 0;
}

bool Locker::UnLock()
{
    return pthread_mutex_unlock(&m_mutex) == 0;
}

pthread_mutex_t* Locker::Get()
{
    return &m_mutex;
}

Cond::Cond()
{
    if(pthread_cond_init(&m_cond,NULL) != 0)
    {
        throw std::exception();
    }
}

Cond::~Cond()
{
    pthread_cond_destroy(&m_cond);
}

bool Cond::Wait(pthread_mutex_t* _mutex)
{
    int ret = 0;
    ret = pthread_cond_wait(&m_cond,_mutex);
    return ret;
}

bool Cond::TimeWait(pthread_mutex_t* _mutex,timespec _t)
{
    int ret = 0;
    ret = pthread_cond_timedwait(&m_cond,_mutex,&_t);
    return ret;
}

bool Cond::Signal()
{
    return pthread_cond_signal(&m_cond) == 0;
}

bool Cond::Broadcast()
{
    return pthread_cond_broadcast(&m_cond) == 0;
}

